﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using Tools;
using Tour.DAL;

namespace Tour.BLL
{
    /// <summary>
    /// Summary description for TblTour
    /// </summary>
    public class TblTourManagerBase
    {
        public TblTourManagerBase()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        public void InsertUpdate(TblTourEntity item)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@Id", item.Id));
            ls.Add(new SqlParameter("@TourTypeId", item.TourTypeId));
            ls.Add(new SqlParameter("@Name", item.Name));
            ls.Add(new SqlParameter("@TextId", item.TextId));
            ls.Add(new SqlParameter("@TravelDay", item.TravelDay));
            ls.Add(new SqlParameter("@BeginDate", item.BeginDate));
            ls.Add(new SqlParameter("@Means", item.Means));
            ls.Add(new SqlParameter("@ImageTour", item.ImageTour));
            ls.Add(new SqlParameter("@IsPromotion", item.IsPromotion));
            ls.Add(new SqlParameter("@PromotionPercent", item.PromotionPercent));
            ls.Add(new SqlParameter("@PromotionPrice", item.PromotionPrice));
            ls.Add(new SqlParameter("@Prices", item.Prices));
            ls.Add(new SqlParameter("@DisplayOrder", item.DisplayOrder));
            ls.Add(new SqlParameter("@IsHome", item.IsHome));
            ls.Add(new SqlParameter("@IsHot", item.IsHot));
            ls.Add(new SqlParameter("@IsVisible", item.IsVisible));
            ls.Add(new SqlParameter("@CreatedBy", item.CreatedBy));
            ls.Add(new SqlParameter("@CreatedDate", item.CreatedDate));
            ls.Add(new SqlParameter("@UpdatedBy", item.UpdatedBy));
            ls.Add(new SqlParameter("@UpdatedDate", item.UpdatedDate));
            SqlParameter[] sqlparam = ls.ToArray();
            SqlHelper.ExecuteNonQuery(SqlHelper.TourConnectString, CommandType.StoredProcedure, "tr_InsertUpdateTblTour", sqlparam);
        }
        public void InsertUpdate(int Id, int TourTypeId, string Name, string TextId, string TravelDay, string BeginDate, string Means, string ImageTour, bool IsPromotion, decimal PromotionPercent, decimal PromotionPrice, decimal Prices, int DisplayOrder, bool IsHome, bool IsHot, bool IsVisible, string CreatedBy, System.DateTime CreatedDate, string UpdatedBy, System.DateTime UpdatedDate)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@Id", Id));
            ls.Add(new SqlParameter("@TourTypeId", TourTypeId));
            ls.Add(new SqlParameter("@Name", Name));
            ls.Add(new SqlParameter("@TextId", TextId));
            ls.Add(new SqlParameter("@TravelDay", TravelDay));
            ls.Add(new SqlParameter("@BeginDate", BeginDate));
            ls.Add(new SqlParameter("@Means", Means));
            ls.Add(new SqlParameter("@ImageTour", ImageTour));
            ls.Add(new SqlParameter("@IsPromotion", IsPromotion));
            ls.Add(new SqlParameter("@PromotionPercent", PromotionPercent));
            ls.Add(new SqlParameter("@PromotionPrice", PromotionPrice));
            ls.Add(new SqlParameter("@Prices", Prices));
            ls.Add(new SqlParameter("@DisplayOrder", DisplayOrder));
            ls.Add(new SqlParameter("@IsHome", IsHome));
            ls.Add(new SqlParameter("@IsHot", IsHot));
            ls.Add(new SqlParameter("@IsVisible", IsVisible));
            ls.Add(new SqlParameter("@CreatedBy", CreatedBy));
            ls.Add(new SqlParameter("@CreatedDate", CreatedDate));
            ls.Add(new SqlParameter("@UpdatedBy", UpdatedBy));
            ls.Add(new SqlParameter("@UpdatedDate", UpdatedDate));
            SqlParameter[] sqlparam = ls.ToArray();
            SqlHelper.ExecuteNonQuery(SqlHelper.TourConnectString, CommandType.StoredProcedure, "tr_InsertUpdateTblTour", sqlparam);
        }
        public string GetUniqueTextIdFromUnicodeText(string UnicodeText)
        {
            string strSql = @"  SELECT TextId FROM TblTour tn
                                WHERE tn.TextId=@TextId";
           string textId = WebUtility.GetUniqueTextIdFromUnicodeText(UnicodeText, "@TextId", strSql
               , SqlHelper.TourConnectString, 255);
           return textId;
        }
        public void Delete(int Id)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@Id", Id));
            SqlParameter[] sqlparam = ls.ToArray();
            SqlHelper.ExecuteNonQuery(SqlHelper.TourConnectString, CommandType.StoredProcedure, "tr_DeleteTblTour", sqlparam);
        }
        public TblTourEntity GetTblTourEntity(DataRow dr)
        {
            TblTourEntity item = new TblTourEntity();            
            if (dr.Table.Columns.Contains("Id"))
                item.Id = dr["Id"].ToInt();              
            if (dr.Table.Columns.Contains("TourTypeId"))
                item.TourTypeId = dr["TourTypeId"].ToInt();              
            if (dr.Table.Columns.Contains("Name"))
                item.Name = dr["Name"].ToString();
            if (dr.Table.Columns.Contains("TourTypeName"))
                item.TourTypeName = dr["TourTypeName"].ToString();  
            if (dr.Table.Columns.Contains("TextId"))
                item.TextId = dr["TextId"].ToString();              
            if (dr.Table.Columns.Contains("TravelDay"))
                item.TravelDay = dr["TravelDay"].ToString();              
            if (dr.Table.Columns.Contains("BeginDate"))
                item.BeginDate = dr["BeginDate"].ToString();              
            if (dr.Table.Columns.Contains("Means"))
                item.Means = dr["Means"].ToString();              
            if (dr.Table.Columns.Contains("ImageTour"))
                item.ImageTour = dr["ImageTour"].ToString();              
            if (dr.Table.Columns.Contains("IsPromotion"))
                item.IsPromotion = dr["IsPromotion"].ToBoolean();              
            if (dr.Table.Columns.Contains("PromotionPercent"))
                item.PromotionPercent = dr["PromotionPercent"].ToDecimal();              
            if (dr.Table.Columns.Contains("PromotionPrice"))
                item.PromotionPrice = dr["PromotionPrice"].ToDecimal();              
            if (dr.Table.Columns.Contains("Prices"))
                item.Prices = dr["Prices"].ToDecimal();              
            if (dr.Table.Columns.Contains("DisplayOrder"))
                item.DisplayOrder = dr["DisplayOrder"].ToInt();              
            if (dr.Table.Columns.Contains("IsHome"))
                item.IsHome = dr["IsHome"].ToBoolean();              
            if (dr.Table.Columns.Contains("IsHot"))
                item.IsHot = dr["IsHot"].ToBoolean();              
            if (dr.Table.Columns.Contains("IsVisible"))
                item.IsVisible = dr["IsVisible"].ToBoolean();              
            if (dr.Table.Columns.Contains("CreatedBy"))
                item.CreatedBy = dr["CreatedBy"].ToString();              
            if (dr.Table.Columns.Contains("CreatedDate"))
                item.CreatedDate = dr["CreatedDate"].ToString().ToDateTime("vi-VN");            
            if (dr.Table.Columns.Contains("UpdatedBy"))
                item.UpdatedBy = dr["UpdatedBy"].ToString();              
            if (dr.Table.Columns.Contains("UpdatedDate"))
                item.UpdatedDate = dr["UpdatedDate"].ToString().ToDateTime("vi-VN");           
            return item;
        }        
        public DataTable SelectOneRDT(int Id)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@Id", Id));
            SqlParameter[] sqlparam = ls.ToArray();
            return SqlHelper.ExecuteDataTable(SqlHelper.TourConnectString, CommandType.StoredProcedure, "tr_SelectTblTour", sqlparam);
        }
        public TblTourEntity SelectOne(int Id)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@Id", Id));
            SqlParameter[] sqlparam = ls.ToArray();
            DataTable dt = SqlHelper.ExecuteDataTable(SqlHelper.TourConnectString, CommandType.StoredProcedure, "tr_SelectTblTour", sqlparam);
            if(dt!=null && dt.Rows.Count>0)
                return GetTblTourEntity(dt.Rows[0]);
            return null;
        }
        public DataTable SelectAllRDT()
        {
            return SqlHelper.ExecuteDataTable(SqlHelper.TourConnectString, CommandType.StoredProcedure, "tr_SelectTblToursAll", null);
        }
        public List<TblTourEntity> SelectAll()
        {
            List<TblTourEntity> lsresult= new List<TblTourEntity>();
            DataTable dt =  SqlHelper.ExecuteDataTable(SqlHelper.TourConnectString, CommandType.StoredProcedure, "tr_SelectTblToursAll", null);
            if (dt != null && dt.Rows.Count > 0)
                for (int i = 0; i < dt.Rows.Count; i++)
                    lsresult.Add(GetTblTourEntity(dt.Rows[i]));            
            return lsresult;
        }
    }
}

